package com.jctrip.hotel.source.controller;

import com.aliyun.openservices.log.util.JsonUtils;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.source.model.atour.AtourIncrNotifyData;
import com.jctrip.hotel.source.service.atour.AtourService;
import com.jctrip.hotel.source.service.atour.AtourSignUtils;
import com.jctrip.hotel.source.service.atour.AtourTaskService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：Haruhi
 * @description:
 * @Date：2023/12/4 10:54
 * @Package：com.jctrip.hotel.source.controller
 * @Project：hotel-main
 */
@RestController
@RequestMapping("/atour/callback")
//@Api(tags = "亚朵增量推送回调接口")
@Slf4j
public class AtourCallBackController {
    @Autowired
    private AtourTaskService atourTaskService;

    /**
     * 接收通知 https://open.jctech-inc.com/main/atour/callback/notify
     *
     * @param json
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public Boolean notify(@RequestBody String json, String sign) {
        // 签名校验
        // sign key由亚朵提供
        String signKey = "b6103b8498d44535973a132ddb9adde5";   // 将 json 内容和 signKey拼接
        String signContent = json + signKey;   // 进行md5获取签名

        String s = DigestUtils.md5DigestAsHex(signContent.getBytes()).toLowerCase();   // 比对签名是否正确
        if (!StringUtils.equals(s, sign)) {
            throw new RuntimeException("签名不正确");
        }
        log.info("接收到增量通知，sign={},json={}", sign, json);
        // 增量结果集
        List<AtourIncrNotifyData> atourIncrNotifyDataList = JsonUtil.toList(AtourIncrNotifyData.class, json);
        atourTaskService.syncAtourIncrCallBackProcess(atourIncrNotifyDataList);

        return true;
    }
}
